/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.hva.web.mvcwebapp.doa;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import nl.hva.web.mvcwebapp.models.User;
import nl.hva.web.mvcwebapp.models.Course;
import nl.hva.web.mvcwebapp.service.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Thijs
 */
public class CourseDAO {

    public CourseDAO() {
    }

    public static List<Course> getAllCourses() {

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Course> course_list = session.createCriteria(Course.class).list();
        tx.commit();

        return course_list;
    }

    public static Course getCourse(int course_id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Course course = (Course) session.get(Course.class, course_id);
        tx.commit();

        //Course course = new Course(1, 1, "Java", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendiere ante, id porttitor ipsum semper non. Etiam mollis euismod dapibus. Phasellus vel turpis sem. Vestibulum dui ...", "1", "http://www.downloadstad.nl/screenshot/Java.jpg");
        return course;
    }

    public static ArrayList insertCourse(HttpServletRequest request, User user) {
        ArrayList validation_errors = new ArrayList();
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Course course = getCoursesFromRequest(request, user);
        session.save(course);
        tx.commit();
        return validation_errors;
    }

    public static ArrayList updateCourse(HttpServletRequest request, User user) {
        ArrayList validation_errors = new ArrayList();
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Course course_update = getCoursesFromRequest(request, user);
        session.update(course_update);
        tx.commit();
        return validation_errors;
    }

    public static void deleteCourse(int user_id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Course tempCourse = (Course) session.load(Course.class, user_id);
        session.delete(tempCourse);
        tx.commit();
    }

    private static Course getCoursesFromRequest(HttpServletRequest request, User user) {
        Course c = new Course();

        if (request.getParameter("courseid") != null && !request.getParameter("courseid").isEmpty()) {
            c.setCourseid(Integer.parseInt(request.getParameter("courseid")));
        }
        if (request.getParameter("coursetitle") != null && !request.getParameter("coursetitle").isEmpty()) {
            c.setCoursetitle(request.getParameter("coursetitle"));
        }
        if (request.getParameter("coursedescription") != null && !request.getParameter("coursedescription").isEmpty()) {
            c.setCoursedescription(request.getParameter("coursedescription"));
        }
        if (request.getParameter("coursepicture") != null && !request.getParameter("coursepicture").isEmpty()) {
            c.setCoursepicture(request.getParameter("coursepicture"));
        }
        if (request.getParameter("coursecategory") != null) {
            c.setCoursecategory(Integer.parseInt(request.getParameter("coursecategory")));
        }
        
        String[] headTeacher = request.getParameterValues("teacherlist");
        System.out.println(headTeacher[0] + " is the selected teacher.");
        c.setHeadteacher(headTeacher[0]);

        return c;
    }
}
